#include "Match.h"

Match::Match(Load *loader)
{
	(*this).loader = loader;
}
void Match::__001_trainOneFile(char *filePath_input, char *filePath_output)
{
	Node readNode;
	FILE *fp_input = fopen(filePath_input, "r");
	FILE *fp_output = fopen(filePath_output, "w");
	map<int, char*> list_getStr;
	Ans getAns;
	char read_one_line[READ_STR_BYTE_IN_ONE_LINE + 2];
	while(fgets(read_one_line, READ_STR_BYTE_IN_ONE_LINE, fp_input) != NULL)
	{
		list_getStr = (*this).__003_myStrtok(read_one_line, ",");
		(*this).__002_fixStringNum(list_getStr[1], NUM_POSITION_FIX);
		(*this).__002_fixStringNum(list_getStr[2], NUM_POSITION_FIX);
		readNode = Node(atof(list_getStr[1]), atof(list_getStr[2]));
		getAns = (*this).loader->ACT003_getSearchResult(readNode);
		fprintf(fp_output, "%s,%d,%.2f\n", list_getStr[0], getAns.pointID, getAns.confidence);
	}
	fclose(fp_input);
	fclose(fp_output);
}
void Match::__002_fixStringNum(char* num, int fixDigit)
{
	int index = 0, fixTime = 0;
	char temp = 'c';
	int isOver = 0;
	//find index of num
	while(num[index] != '.')
	{
		index = index + 1;
	}
	//fix the number to 6 presion double
	while(fixTime < fixDigit)
	{
		if(isOver == 0)
		{
		    temp = num[index];
			num[index] = num[index + 1];
			num[index + 1] = temp;
		}
		else if(isOver == 1)
		{
			isOver = 2;
			num[index] = '0';
		}
		else
		{
			num[index] = '0';
			num[index + 1] = '\0';
		}
		index = index + 1;
		if(isOver == 0 && num[index + 1] == '\0')
		{
			isOver = 1;
		}
		fixTime = fixTime + 1;
	}
}
map<int, char*> Match::__003_myStrtok(char *stringList, char *delimiter)
{
	map<int, char*> list_str;
	char *getTok = NULL;
	int index = 0;
	getTok = strtok(stringList, delimiter);
	while(getTok != NULL)
	{
		list_str[index] = getTok;
		getTok = strtok(NULL, delimiter);
		index = index + 1;
	}
	return list_str;
}
void Match::_004_replaceSubString(char *orgStr, char *oldSubStr, char *newSubStr, char *newStr)
{
	int index_orgStr = 0, index_oldSubStr = 0, index_newSubStr = 0, indexNewStr = 0;
	int replaceIndex = 0;
	int state = -1;
	bool isFinish = false;
	if(strlen(orgStr) > 0)
	{
		while(!isFinish && orgStr[index_orgStr] != '\0')
		{
			if(orgStr[index_orgStr] == oldSubStr[index_oldSubStr])  //the same
			{
				index_oldSubStr = index_oldSubStr + 1;
				if(oldSubStr[index_oldSubStr] == '\0')
				{
					isFinish = true;
					replaceIndex = index_orgStr - strlen(oldSubStr) + 1;
				}
			}
			else  //different
			{
				if(index_oldSubStr > 0)
				{
					index_oldSubStr = 0;
					index_orgStr = index_orgStr - 1;
				}
			}
			index_orgStr = index_orgStr + 1;
		}
		//start to assign new string
		indexNewStr = 0;
		index_orgStr = 0;
		index_newSubStr = 0;
		state = STATE_STR_REPLACE_ORG;
		while(orgStr[index_orgStr] != '\0')
		{
			//judge state of assigning word
			if(indexNewStr == replaceIndex)
			{
				state = STATE_STR_REPLACE_SUB;
			}
			else if(state == STATE_STR_REPLACE_SUB && newSubStr[index_newSubStr] == '\0')
			{
				state = STATE_STR_REPLACE_ORG;
				index_orgStr = index_orgStr + strlen(oldSubStr);
			}
			//assign according to current state
			if(state == STATE_STR_REPLACE_ORG)
			{
				newStr[indexNewStr] = orgStr[index_orgStr];
				index_orgStr = index_orgStr + 1;
			}
			else
			{
				newStr[indexNewStr] = newSubStr[index_newSubStr];
				index_newSubStr = index_newSubStr + 1;
			}
			indexNewStr = indexNewStr + 1;
		}
		newStr[indexNewStr] = '\0';
	}
}
void Match::ACT001_startTrainData(char *dirPath_input, char* dirPath_output)
{
    char sdir[256], filter[256], path_in[256], path_out[256], newStr[TRAIN_FILENAME_LEN];
    struct _finddata_t c_file;
    long hFile;
    strcpy( sdir, dirPath_input);
    strcpy( filter, "*.txt" );
    _chdir( sdir );
    hFile = _findfirst(filter, &c_file);
    if( hFile!=-1 )
    {
        do
	    {
            sprintf(path_in, "%s/%s", dirPath_input, c_file.name );
			(*this)._004_replaceSubString(c_file.name, "in", "out", newStr);
			sprintf(path_out, "%s/%s", dirPath_output, newStr);
			printf("Start to map data - %s\n", c_file.name);
			(*this).__001_trainOneFile(path_in, path_out);
			printf("Mapping finishes, result data - %s is generated\n", newStr);
        }while( _findnext( hFile, &c_file )==0 );	
    }
}